package com.ljq.software_springboot.softwareprogram.imp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ljq.software_springboot.softwareprogram.entity.Account;
import com.ljq.software_springboot.softwareprogram.entity.GroupMember;
import com.ljq.software_springboot.softwareprogram.entity.UserGroup;
import com.ljq.software_springboot.softwareprogram.mapper.GroupMemberMapper;
import com.ljq.software_springboot.softwareprogram.mapper.UserGroupMapper;
import com.ljq.software_springboot.softwareprogram.service.IUserGroupService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-28
 */
@Service
public class UserGroupServiceImpl extends ServiceImpl<UserGroupMapper, UserGroup> implements IUserGroupService {
    @Autowired
    UserGroupMapper groupMapper;
    @Autowired
    GroupMemberMapper groupMemberMapper;
    public String createGroup(UserGroup usergroup) {
        // 查询组
        LambdaQueryWrapper<UserGroup> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserGroup::getName, usergroup.getName());
        UserGroup account = groupMapper.selectOne(queryWrapper) ;

        // 用户不存在
        if (account != null) {
            return "该组存在";
        }
        // 保存用户信息到数据库
        UserGroup group = new UserGroup();
        GroupMember groupMember = new GroupMember();
        group.setName(usergroup.getName());
        group.setCreatorId(usergroup.getCreatorId());
        groupMapper.insert(group);
        groupMember.setGroupId(group.getId());
        groupMemberMapper.insert(groupMember);

        return "success";
    }
    public List<UserGroup> loadFriendGroups(UserGroup group ){
        LambdaQueryWrapper<UserGroup> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserGroup::getCreatorId, group.getCreatorId());
        return groupMapper.selectList(queryWrapper);
    }
}
